/*
 * @Author: wangtao
 * @Date: 2020-12-23 14:58:12
 * @LastEditors: 汪滔
 * @LastEditTime: 2021-07-16 16:08:42
 * @Description: file content
 */
import React, { Component } from 'react';
import { StyleSheet } from 'react-native';
import { mainBgColorWhite } from '@/styles';
import { msg } from '@/common';
import {} from '@/images';

import api from '@/api';
import GenerateContract from './components/generate-contract';
import ContractContent from './components/contract-content';

export default class ContractDetail extends Component {
  constructor(props) {
    super(props);
    this.state = {
      contractStatus: 1, // 合同状态 后续根据后端接口变化  1:生成合同  2：签订合同 4: 查看合同并且查看上链key值
      contractData: {},
      routerFrom: '', // 判断合同入口
    };
  }

  static getDerivedStateFromProps(nextProps, preState) {
    const { navigation } = nextProps;
    const state = navigation.state || {};
    const params = state.params || {};
    const { contractStatus, data, routerFrom } = params;
    if (routerFrom === 'contractFeedback') {
      return { contractStatus, contractData: data };
    }
    return state;
  }

  componentDidMount() {
    const { navigation } = this.props;
    const state = navigation.state || {};
    const params = state.params || {};
    const { contractStatus, data, routerFrom, contractId } = params;
    this.setState({ contractStatus, routerFrom }, () => {
      if (routerFrom === 'DriverMain') {
        // 司机端首页 保密协议
        api.contract
          .createConfidentialityContract({ legalCustId: data.enterpriseId })
          .then((res) => {
            if (res.success) {
              api.contract.findMessagePersonPhone().then((resData) => {
                if (resData.success) {
                  const contractData = res.data;
                  contractData.mobile = resData.data;
                  this.setState({ contractStatus: 2, contractData });
                } else {
                  msg.emit('app:tip', { text: res.msg });
                  msg.emit('router: back');
                }
              });
            } else {
              msg.emit('app:tip', { text: res.msg });
              msg.emit('router: back');
            }
          });
      } else {
        // 车辆列表详情中查看合同
        if (routerFrom === 'ownerCarDetail') {
          api.contract.findByContractNo(contractId).then((res) => {
            if (res.success) {
              this.setState({ contractData: res.data });
            } else {
              msg.emit('app:tip', { text: res.msg });
              msg.emit('router: back');
            }
          });
        } else {
          if (contractStatus === 4) {
            // 查看合同
            this.setState({ contractData: data, contractStatus });
            console.log('=====================contract-detail=====>', data);
          } else {
            // 签署合同
            if (routerFrom === 'ContractList') {
              api.contract.findMessagePersonPhone().then((resData) => {
                if (resData.success) {
                  const contractData = data;
                  contractData.mobile = resData.data;
                  this.setState({ contractStatus: 2, contractData });
                } else {
                  msg.emit('app:tip', { text: res.msg });
                  msg.emit('router: back');
                }
              });
            } else {
              api.contract.appCreateCarContract({ ...data }).then((res) => {
                if (res.success) {
                  api.contract.findMessagePersonPhone().then((resData) => {
                    if (resData.success) {
                      const contractData = res.data;
                      contractData.mobile = resData.data;
                      this.setState({ contractStatus: 2, contractData });
                    } else {
                      msg.emit('app:tip', { text: res.msg });
                      msg.emit('router: back');
                    }
                  });
                } else {
                  msg.emit('app:tip', { text: res.msg });
                  msg.emit('router: back');
                }
              });
            }
          }
        }
      }
    });
  }

  render() {
    const { contractStatus, contractData, routerFrom } = this.state;
    if (contractStatus === 1) {
      return <GenerateContract contractStatus={contractStatus} />;
    }
    if (contractStatus === 2 || contractStatus === 4) {
      return (
        <ContractContent
          contractStatus={contractStatus}
          contractData={contractData}
          routerFrom={routerFrom}
          {...this.props}
        />
      );
    }
    return null;
  }
}
const styles = StyleSheet.create({
  container: {
    backgroundColor: mainBgColorWhite,
    height: '100%',
    width: '100%',
  },
});
